package com.weipai.demo.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.weipai.demo.enums.RetCode;
import com.weipai.demo.mapper.base.CreditCardMapper;
import com.weipai.demo.model.CreditCard;
import com.weipai.demo.service.CreditCardService;
import com.weipai.demo.util.LayuiTablePagingResult;
import com.weipai.demo.util.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: 赵项阳
 * @Date: 2019/6/29 16:05
 * @Version 1.0
 */
@Service
public class CreditCardServiceImpl implements CreditCardService {

    private static final Logger log = LoggerFactory.getLogger(CreditCardServiceImpl.class);

    @Autowired
    private CreditCardMapper creditCardMapper;


    //添加或修改信用卡分类
    @Override
    public Response cardClass(String designation, String attribute, Integer status,Integer id) {
        Response response=new Response();
        CreditCard creditCard=new CreditCard();
        //判断id为空时,添加
        if (id==null){
            creditCard.setAttribute(attribute);
            creditCard.setDesignation(designation);
            creditCard.setStatus(status);
            if (creditCard!=null){
                int insert = creditCardMapper.insert(creditCard);
                if (insert==1){
                    response.setCode(RetCode.SUCCESS);
                }else {
                    response.setCode(RetCode.REQUEST_ERROR);
                    return response;
                }
            }
        }else {
            creditCard.setAttribute(attribute);
            creditCard.setDesignation(designation);
            creditCard.setStatus(status);
            creditCard.setId(id);
            int i = creditCardMapper.updateByPrimaryKey(creditCard);
            if (i==1){
                response.setCode(RetCode.UPDATESUCCESS);
            }else {
                response.setCode(RetCode.REQUEST_ERROR);
                return response;
            }
        }
       
        return response;
    }

    //分页展示信用卡分类列表
    @Override
    public LayuiTablePagingResult pageShow(Integer page, Integer size,CreditCard creditCard) {
        LayuiTablePagingResult result=null;

        try {
            PageHelper.startPage(page,size);

            List<CreditCard> creditCards = creditCardMapper.selectAll();

            PageInfo<CreditCard>pageInfo=new PageInfo<>(creditCards);
            result=new LayuiTablePagingResult();
            result.setCode(RetCode.SUCCESS.getCode());
            result.setMsg(RetCode.SUCCESS.getMsg());
            result.setCount(pageInfo.getTotal());
            result.setData(pageInfo.getList());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("pageShow"+e.getMessage());
        }

        return result;
    }

    //删除信用卡消息
    @Override
    public Response deleteById(Integer id) {
        Response response=new Response();
        if (id!=null){
            int i = creditCardMapper.deleteByPrimaryKey(id);
            response.setCode(RetCode.SUCCESS);
        }
        return response;
    }

    //按照关键字查询
    @Override
    public LayuiTablePagingResult searchBykeyword(Integer page, Integer size, CreditCard creditCard) {
        LayuiTablePagingResult result=null;

        try {
            PageHelper.startPage(page,size);

            List<CreditCard> creditCards = creditCardMapper.select(creditCard);

            PageInfo<CreditCard> pageInfo=new PageInfo<>(creditCards);
            result = new LayuiTablePagingResult();
            result.setCode(RetCode.SUCCESS.getCode());
            result.setMsg(RetCode.SUCCESS.getMsg());
            result.setCount(pageInfo.getTotal());
            result.setData(pageInfo.getList());

        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
        }

        return result;
    }

}
